package com.damocode.starter.httpclient.domain;

import com.damocode.starter.httpclient.autoconfigure.HttpClientPoolProperties;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.util.*;

public class UrlEncodedFormEntityRequest extends HttpRequest{

    private final Map<String, String> parameters = new HashMap();

    public UrlEncodedFormEntityRequest(String url) {
        super(url);
    }

    @Override
    protected void buildRequestBuilder(HttpClientPoolProperties httpClientPoolProperties, RequestBuilder builder) {
        List<NameValuePair> pairList = new ArrayList<>(parameters.size());
        parameters.forEach((k,v) -> {
            NameValuePair pair = new BasicNameValuePair(k, v);
            pairList.add(pair);
        });
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairList, Consts.UTF_8);
        builder.setEntity(entity);
    }

    public Map<String, String> getParameters() {
        return Collections.unmodifiableMap(this.parameters);
    }

    protected void putParameter(String name, Object value) {
        this.setParameter(this.parameters, name, value);
    }
}
